import 'dart:io';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shop/common/baseContext.dart';
import 'package:shop/routers/Router.dart';

//图片封装 , 网络图片未加载出来时 , 有默认图片, 本地缓存
// by: yrs

class MyImage {
  static Widget network(String src,
      {double height,
      double width,
      BoxShape shape,
      BoxFit fit = BoxFit.cover,
      BoxBorder border,
      bool cache = true,
      BorderRadius borderRadius,
      bool magnify = false}) {
    return GestureDetector(
      onTap: () {
        if (magnify) {
          Navigator.push(
              BaseContext.baseContext,
              Platform.isAndroid
                  ? TransparentMaterialPageRoute(
                      builder: (_) => ImageDital(arguments: src))
                  : TransparentCupertinoPageRoute(
                      builder: (_) => ImageDital(arguments: src)));
        }
      },
      child: Hero(
        tag: "$src",
        child: ExtendedImage.network(
          src,
          fit: fit,
          height: height == null ? null : height,
          width: width == null ? null : width,
          shape: shape,
          border: border,
          cache: cache,
          borderRadius: borderRadius,
          loadStateChanged: (ExtendedImageState state) {
            switch (state.extendedImageLoadState) {
              case LoadState.loading:
                return Text("加载中");
              case LoadState.completed:
                return state.completedWidget;
              case LoadState.failed:
                magnify = false;
                return Container(
                  child: Text("加载失败"),
                );
            }
            return null;
          },
        ),
      ),
    );
  }
}

class ImageDital extends StatefulWidget {
  final String arguments;
  ImageDital({Key key, this.arguments}) : super(key: key);
  @override
  _ImageDitalState createState() => _ImageDitalState();
}

class _ImageDitalState extends State<ImageDital> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => route.pop(),
      child: Scaffold(
        body: Container(
          color: Colors.black,
          child: Center(
            child: Hero(
                tag: "${widget.arguments}",
                child: ExtendedImage.network(
                  widget.arguments,
                  height: MediaQuery.of(context).size.height,
                  width: MediaQuery.of(context).size.width,
                  mode: ExtendedImageMode.gesture,
                  initGestureConfigHandler: (state) {
                    return GestureConfig(
                      minScale: 0.9,
                      animationMinScale: 0.7,
                      maxScale: 3.0,
                      animationMaxScale: 3.5,
                      speed: 1.0,
                      inertialSpeed: 100.0,
                      initialScale: 1.0,
                      inPageView: false,
                      initialAlignment: InitialAlignment.center,
                    );
                  },
                  onDoubleTap: (ExtendedImageGestureState state) {
                    state.reset();
                  },
                )),
          ),
        ),
      ),
    );
  }
}
